package com.youlv.yl.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.youlv.yl.config.RedisKeyConfig;
import com.youlv.yl.config.UserScoreConfig;
import com.youlv.yl.dao.UserScoreLogDao;
import com.youlv.yl.entity.user.UserLevel;
import com.youlv.yl.dao.UserLevelDao;
import com.youlv.yl.entity.user.UserScoreLog;
import com.youlv.yl.service.intf.UserLevelService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.youlv.yl.util.JedisUtils;
import com.youlv.yl.util.StringUtils;
import com.youlv.yl.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author fenghe
 * @since 2022-01-12
 */
@Service
public class UserLevelServiceImpl extends ServiceImpl<UserLevelDao, UserLevel> implements UserLevelService {
    @Autowired
    private UserLevelDao dao;
    @Autowired
    private UserScoreLogDao scoreDao;

    @Override
    public int save(Long uid, int score, String msg) {
        // 1查询 当前的等级，记录日志
        scoreDao.insert(new UserScoreLog(uid, score, msg));
        UserLevel level = dao.selectOne(new QueryWrapper<UserLevel>().eq("uid", uid));
        if(level != null) {
            // 2.校验，当前积分的变动，是否影响等级
            level.setScore(level.getScore() + score);
            level.setCurrScore(level.getCurrScore() + score);
            // 对比当前积分
            switch (level.getLevel()) {
                case UserScoreConfig.USER_SCORE_LEVEL_5: {
                    if(level.getScore() > UserScoreConfig.USER_SCORE_COUNT_6) {
                        level.setLevel(UserScoreConfig.USER_SCORE_LEVEL_6);
                    }
                    break;
                }
                case UserScoreConfig.USER_SCORE_LEVEL_4: {
                    if(level.getScore() > UserScoreConfig.USER_SCORE_COUNT_5) {
                        level.setLevel(UserScoreConfig.USER_SCORE_LEVEL_5);
                    }
                    break;
                }
                case UserScoreConfig.USER_SCORE_LEVEL_3: {
                    if(level.getScore() > UserScoreConfig.USER_SCORE_COUNT_4) {
                        level.setLevel(UserScoreConfig.USER_SCORE_LEVEL_4);
                    }
                    break;
                }
                case UserScoreConfig.USER_SCORE_LEVEL_2: {
                    if(level.getScore() > UserScoreConfig.USER_SCORE_COUNT_3) {
                        level.setLevel(UserScoreConfig.USER_SCORE_LEVEL_3);
                    }
                    break;
                }
                case UserScoreConfig.USER_SCORE_LEVEL_1: {
                    if(level.getScore() > UserScoreConfig.USER_SCORE_COUNT_2) {
                        level.setLevel(UserScoreConfig.USER_SCORE_LEVEL_2);
                    }
                    break;
                }
            }
            return dao.updateById(level);
        } else {
            level = new UserLevel(uid, score, score, UserScoreConfig.USER_SCORE_LEVEL_1);
            return dao.insert(level);
        }
    }

    /** 查询当前用户积分等级*/
    @Override
    public R queryScore(String token) {
        // 入参验证
        if(StringUtils.isNotEmpty(token)) {
            // 通过token获取uid
            long id = Long.parseLong(JedisUtils.getString(RedisKeyConfig.USER_TOKEN + token));
            // 通过id查询用户信息
            QueryWrapper<UserLevel> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("uid", id);
            return R.ok(dao.selectOne(queryWrapper));
        }
        return null;
    }
}
